package com.dianshang.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dianshang.dao.AlbumDao;
import com.dianshang.entity.PageResult;
import com.dianshang.pojo.goods.Album;
import com.dianshang.service.goods.AlbumService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumDao albumDao;

    /**
     * 查询所有
     */
    @Override
    public List<Album> findAll() {
        return albumDao.selectAll();
    }

    /**
     * 查询所有+分页
     * page 页码
     * size 每页记录数
     * 分页结果
     */
    @Override
    public PageResult<Album> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Page<Album> albums = (Page<Album>) albumDao.selectAll();
        return new PageResult<Album>(albums.getTotal(),albums.getResult());
    }

    /**
     * 查询所有+模糊
     * searchMap 查询条件
     */
    @Override
    public List<Album> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return albumDao.selectByExample(example);
    }

    /**
     * 查询所有+分页+模糊
     * searchMap
     * page
     * size
     */
    @Override
    public PageResult<Album> findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Album> albums = (Page<Album>) albumDao.selectByExample(example);
        return new PageResult<Album>(albums.getTotal(),albums.getResult());
    }

    /**
     * 回显
     * id
     */
    @Override
    public Album findById(Integer id) {
        return albumDao.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * album
     */
    @Override
    public void add(Album album) {
        album.setImageItems("[]");
        albumDao.insert(album);
    }

    /**
     * 修改
     * album
     */
    @Override
    public void update(Album album) {
        albumDao.updateByPrimaryKeySelective(album);
    }

    /**
     * 删除
     * id
     */
    @Override
    public void delete(Integer id) {
        albumDao.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * searchMap
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Album.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap != null){
            // 相册名称
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
                criteria.andLike("title","%"+searchMap.get("title")+"%");
            }
            // 相册封面
            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
                criteria.andLike("image","%"+searchMap.get("image")+"%");
            }
            // 图片列表
            if(searchMap.get("imageItems")!=null && !"".equals(searchMap.get("imageItems"))){
                criteria.andLike("imageItems","%"+searchMap.get("imageItems")+"%");
            }
            // 编号
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
        }
        return example;
    }

}